<?
class ccdev extends module {

    // Author: Herman Tolentino MD
    // CHITS Project 2004

    function ccdev() {
        //
        // do not forget to update version
        //
        $this->author = 'Herman Tolentino MD';
        $this->version = "0.2-".date("Y-m-d");
        $this->module = "ccdev";
        $this->description = "CHITS Module - Child Care Dev";

    }

    // --------------- STANDARD MODULE FUNCTIONS ------------------

    function init_deps() {
    //
    // insert dependencies in module_dependencies
    //
        module::set_dep($this->module, "module");
        module::set_dep($this->module, "healthcenter");
        module::set_dep($this->module, "patient");
        module::set_dep($this->module, "complaint");
        module::set_dep($this->module, "lab");
        module::set_dep($this->module, "vaccine");
        module::set_dep($this->module, "occupation");
        module::set_dep($this->module, "education");
        module::set_dep($this->module, "barangay");
        module::set_dep($this->module, "family");
        module::set_dep($this->module, "imci");

    }

    function init_lang() {
    //
    // insert necessary language directives
    //
        module::set_lang("FTITLE_CCDEV_SERVICE_FORM", "english", "CHILD CARE SERVICE FORM", "Y");
        module::set_lang("LBL_SERVICE_ID", "english", "SERVICE ID", "Y");
        module::set_lang("LBL_SERVICE_NAME", "english", "SERVICE NAME", "Y");
        module::set_lang("FTITLE_CCDEV_SERVICES_LIST", "english", "CHILD CARE SERVICES", "Y");
        module::set_lang("THEAD_ID", "english", "ID", "Y");
        module::set_lang("THEAD_NAME", "english", "NAME", "Y");
        module::set_lang("FTITLE_CCDEV_DATA_FORM", "english", "CHILD CARE DATA FORM", "Y");
        module::set_lang("LBL_MOTHERS_OCCUPATION", "english", "MOTHER\'S OCCUPATION", "Y");
        module::set_lang("LBL_MOTHERS_EDUCATION", "english", "MOTHER\'S EDUCATION", "Y");
        module::set_lang("LBL_FATHERS_NAME", "english", "FATHER\'S NAME", "Y");
        module::set_lang("LBL_FATHERS_OCCUPATION", "english", "FATHER\'S OCCUPATION", "Y");
        module::set_lang("LBL_FATHERS_EDUCATION", "english", "FATHER\'S EDUCATION", "Y");
        module::set_lang("LBL_BIRTH_WEIGHT", "english", "BIRTH WEIGHT (kg)", "Y");
        module::set_lang("LBL_CIVIL_REGISTRATION_DATE", "english", "DATE OF REGISTRATION CIVIL REGISTRY", "Y");
        module::set_lang("LBL_DELIVERY_LOCATION", "english", "LOCATION OF DELIVERY", "Y");
        module::set_lang("FTITLE_CCDEV_SERVICES", "english", "CHILD CARE SERVICES", "Y");
        module::set_lang("LBL_SERVICES", "english", "SERVICES", "Y");
        module::set_lang("LBL_VACCINATIONS", "english", "VACCINATIONS", "Y");

    }

    function init_stats() {
    }

    function init_help() {
    }

    function init_menu() {
        // use this for updating menu system
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        // menu entries
        module::set_menu($this->module, "Child Care", "STATS", "_ccdev_stats");
        module::set_menu($this->module, "CCDEV Services", "LIBRARIES", "_ccdev_services");

        // add more detail
        module::set_detail($this->description, $this->version, $this->author, $this->module);

    }

    function init_sql() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        module::execsql("CREATE TABLE `m_consult_ccdev_services` (".
            "`ccdev_id` float NOT NULL default '0',".
            "`consult_id` float NOT NULL default '0',".
            "`user_id` float NOT NULL default '0',".
            "`patient_id` float NOT NULL default '0',".
            "`ccdev_timestamp` timestamp(14) NOT NULL,".
            "`service_id` varchar(10) NOT NULL default '',".
            "PRIMARY KEY  (`ccdev_id`,`consult_id`,`service_id`,`ccdev_timestamp`),".
            "KEY `key_patient` (`patient_id`),".
            "KEY `key_user` (`user_id`)".
            ") TYPE=InnoDB; ");

        module::execsql("CREATE TABLE `m_consult_ccdev_vaccine` (".
            "`ccdev_id` float NOT NULL default '0',".
            "`consult_id` float NOT NULL default '0',".
            "`vaccine_timestamp` timestamp(14) NOT NULL,".
            "`vaccine_id` varchar(10) NOT NULL default '',".
            "PRIMARY KEY  (`ccdev_id`,`consult_id`,`vaccine_id`, `vaccine_timestamp`),".
            "KEY `key_patient` (`patient_id`),".
            "KEY `key_vaccine` (`vaccine_id`),".
            "KEY `key_user` (`user_id`)".
            ") TYPE=InnoDB; ");

        module::execsql("CREATE TABLE `m_lib_ccdev_services` (".
            "`service_id` varchar(10) NOT NULL default '',".
            "`service_name` varchar(50) NOT NULL default '',".
            "PRIMARY KEY  (`service_id`)".
            ") TYPE=InnoDB; ");

        // load initial data
        module::execsql("insert into m_lib_ccdev_services (service_id, service_name) values ('DENT', 'Dental Checkup')");
        module::execsql("insert into m_lib_ccdev_services (service_id, service_name) values ('WORM', 'Deworming')");
        module::execsql("insert into m_lib_ccdev_services (service_id, service_name) values ('VITA', 'Vitamin A')");
        module::execsql("insert into m_lib_ccdev_services (service_id, service_name) values ('NBS', 'Newborn Screening')");

        module::execsql("CREATE TABLE `m_patient_ccdev` (".
            "`ccdev_id` float NOT NULL auto_increment,".
            "`patient_id` varchar(100) NOT NULL default '',".
            "`mother_name` varchar(100) NOT NULL default '',".
            "`mother_educ_id` varchar(10) NOT NULL default '',".
            "`mother_occup_id` varchar(10) NOT NULL default '',".
            "`father_name` varchar(100) NOT NULL default '',".
            "`father_educ_id` varchar(10) NOT NULL default '',".
            "`father_occup_id` varchar(100) NOT NULL default '',".
            "`ccdev_timestamp` timestamp(14) NOT NULL,".
            "`ccdev_dob` date NOT NULL default '0000-00-00',".
            "`birth_weight` float NOT NULL default '0',".
            "`delivery_location` varchar(100) NOT NULL default '',".
            "`civil_registry_date` date NOT NULL default '0000-00-00',".
            "PRIMARY KEY  (`ccdev_id`)".
            ") TYPE=InnoDB;");

        module::execsql("CREATE TABLE `m_patient_ccdev_sibling` (".
            "`ccdev_id` float NOT NULL default '0',".
            "`patient_id` float NOT NULL default '0',".
            "`sibling_patient_id` float NOT NULL default '0',".
            "PRIMARY KEY  (`ccdev_id`,`sibling_patient_id`)".
            ") TYPE=InnoDB; ");

    }

    function drop_tables() {

        module::execsql("DROP TABLE `m_consult_ccdev_services`;");
        module::execsql("DROP TABLE `m_consult_ccdev_vaccine`;");
        module::execsql("DROP TABLE `m_lib_ccdev_services`;");
        module::execsql("DROP TABLE `m_patient_ccdev`;");
        module::execsql("DROP TABLE `m_patient_ccdev_sibling`;");
    }


    // --------------- CUSTOM MODULE FUNCTIONS ------------------

    function _consult_ccdev() {
    //
    // main submodule for ccdev consults
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ccdev')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $c = new ccdev;
        $imci = new imci;

        $c->ccdev_menu($menu_id, $post_vars, $get_vars);
        if ($post_vars["submitccdev"]) {
            $c->process_ccdev($menu_id, $post_vars, $get_vars);
        }
        switch ($get_vars["ccdev"]) {
        case "VISIT1":
            if (!($c->registry_record_exists($get_vars["consult_id"]))) {
                $c->form_ccdev_firstvisit($menu_id, $post_vars, $get_vars, $validuser, $isadmin);
            } else {
                $c->display_ccdev_firstvisit($menu_id, $post_vars, $get_vars, $validuser, $isadmin);
            }
            break;
        case "SIBLINGS":
            patient::newsearch($menu_id, $post_vars, $get_vars, $validuser, $isadmin);
            break;
        case "IMCI":
            break;
        case "SVC":
        default:
            $c->form_ccdev_services($menu_id, $post_vars, $get_vars, $validuser, $isadmin);
            break;
        }

    }

    function form_ccdev_services() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $ccdev_id = ccdev::registry_record_exists($get_vars["consult_id"]);
        print "<table width='300'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=SVC' name='form_ccdev_services' method='post'>";
        print "<tr valign='top'><td>";
        print "<b>".FTITLE_CCDEV_SERVICES."</b><br/><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        // SERVICE
        print "<table bgcolor='#FFFF66' width='300' cellpadding='3'><tr><td>";
        print "<span class='boxtitle'>".LBL_SERVICES."</span><br> ";
        print ccdev::checkbox_services();
        print "</td></tr>";
        print "</table>";

        print "</td></tr>";
        print "<tr valign='top'><td>";
        // VACCINE DATA
        print "<table bgcolor='#CCCCFF' width='300' cellpadding='3'><tr><td>";
        print "<span class='boxtitle'>".LBL_VACCINATIONS."</span><br> ";
        print ccdev::checkbox_vaccines($get_vars["consult_id"]);
        print "</td></tr>";
        print "</table>";

        print "</td></tr>";
        print "<tr><td><br/>";
        if ($_SESSION["priv_add"]) {
            print "<input type='hidden' name='ccdev_id' value='$ccdev_id'>";
            print "<input type='submit' value = 'Update Record' class='textbox' name='submitccdev' style='border: 1px solid #000000'> ";
            print "<input type='submit' value = 'Carry Out' class='textbox' name='submitccdev' style='border: 1px solid #000000'> ";
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function get_age_weeks() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $patient_id = $arg_list[0];
        }
        $sql = "select round((to_days(sysdate())-to_days(patient_dob))/7,1) age from m_patient where patient_id = '$patient_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($age) = mysql_fetch_array($result);
                return $age;
            }
        }
    }

    function check_vaccine_status() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $vaccine_id = $arg_list[0];
            $patient_id = $arg_list[1];
        }
        $sql = "select count(vaccine_id) from m_consult_ccdev_vaccine ".
               "where patient_id = '$patient_id' and vaccine_id = '$vaccine_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                if (list($shots) = mysql_fetch_array($result)) {
                    if ($shots>0) {
                        return "<font color='blue'>GIVEN ($shots)</font>";
                    } else {
                        return "<font color='red'><b>NOT GIVEN</b></font>";
                    }
                }
            }
        }
    }

    function check_vaccine_timeliness() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $vaccine_id = $arg_list[0];
            $patient_id = $arg_list[1];
            $age_weeks = $arg_list[2];
            $interval = $arg_list[3];
        }
        $period = explode(",", $interval);
        $sql = "select vaccine_id from m_consult_ccdev_vaccine ".
               "where patient_id = '$patient_id' and vaccine_id = '$vaccine_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                if (list($shots) = mysql_fetch_array($result)) {
                    foreach($period as $key=>$value) {
                        print $period[$key]."/".$age_weeks."<br/>";
                    }
                    return "<font color='blue'>GIVEN ($shots)</font>";
                }
            } else {
            }
        }
    }

    function checkbox_vaccines() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $consult_id = $arg_list[0];
        }

        $patient_id = healthcenter::get_patient_id($consult_id);
        $age_weeks = ccdev::get_age_weeks($patient_id);

        $sql = "select vaccine_id, vaccine_interval, vaccine_name from m_lib_vaccine order by vaccine_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $interval, $name) = mysql_fetch_array($result)) {
                    $vaccine_status = ccdev::check_vaccine_status($id, $patient_id);
                    //$timely_status = ccdev::check_vaccine_timeliness($id, $patient_id, $age_weeks, $interval);
                    $ret_val .= "<input type='checkbox' name='vaccine[]' value='$id'> $name $vaccine_status<br>";
                }
                return $ret_val;
            }
        }
    }

    function registry_record_exists() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $consult_id = $arg_list[0];
        }
        $patient_id = healthcenter::get_patient_id($consult_id);
        $sql = "select ccdev_id from m_patient_ccdev where patient_id = '$patient_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($id) = mysql_fetch_array($result);
                return $id;
            }
        }
    }

    function ccdev_menu() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        if (!isset($get_vars["ccdev"])) {
            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=SVC");
            //$get_vars["ccdev"] = "SVC";
        }
        print "<table cellpadding='1' cellspacing='1' width='300' bgcolor='#9999FF' style='border: 1px solid black'><tr valign='top'><td nowrap>";
        print "<a href='".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=VISIT1' class='groupmenu'>".strtoupper(($get_vars["ccdev"]=="VISIT1"?"<b>FIRST VISIT</b>":"FIRST VISIT"))."</a>";
        print "<a href='".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=SIBLINGS' class='groupmenu'>".strtoupper(($get_vars["ccdev"]=="SIBLINGS"?"<b>SIBLINGS</b>":"SIBLINGS"))."</a>";
        print "<a href='".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=SVC' class='groupmenu'>".strtoupper(($get_vars["ccdev"]=="SVC"?"<b>SERVICES</b>":"SERVICES"))."</a>";
        print "<a href='".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=IMCI' class='groupmenu'>".strtoupper(($get_vars["ccdev"]=="IMCI"?"<b>IMCI</b>":"IMCI"))."</a>";
        print "</td></tr></table><br/>";
    }

    function process_ccdev() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        switch($post_vars["submitccdev"]) {
        case "Save Child Care Data":
            if ($post_vars["mother_name"] && $post_vars["mother_occup"] &&
                $post_vars["mother_educ"] && $post_vars["delivery_location"]) {
                if ($post_vars["civil_regdate"]) {
                    list($month,$day,$year) = explode("/", $post_vars["civil_regdate"]);
                    $regdate = $year."-".str_pad($month, 2, "0", STR_PAD_LEFT)."-".str_pad($day, 2, "0", STR_PAD_LEFT);
                }
                $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
                $patient_dob = patient::get_dob($patient_id);
                print $sql = "insert into m_patient_ccdev (patient_id, mother_name, mother_educ_id, mother_occup_id, father_name, father_educ_id, father_occup_id, ccdev_timestamp, ccdev_dob, birth_weight, delivery_location, civil_registry_date) ".
                       "values ('$patient_id', '".$post_vars["mother_name"]."', '".$post_vars["mother_educ"]."', '".$post_vars["mother_occup"]."', ".
                       "'".$post_vars["father_name"]."', '".$post_vars["father_educ"]."', '".$post_vars["father_occup"]."', ".
                       "sysdate(), '$patient_dob', '".$post_vars["birth_weight"]."', '".$post_vars["delivery_location"]."', '$regdate')";
                if ($result = mysql_query($sql)) {
                    header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=VISIT1");
                }
            }
            break;
        case "Update Record":
            if ($post_vars["services"]) {
                $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
                foreach($post_vars["services"] as $key=>$value) {
                    $sql = "insert into m_consult_ccdev_services (ccdev_id, consult_id, user_id, patient_id, ccdev_timestamp, service_id) ".
                           "values ('".$post_vars["ccdev_id"]."', '".$get_vars["consult_id"]."', '".$_SESSION["userid"]."', '$patient_id', sysdate(), '$value')";
                    $result = mysql_query($sql);
                }
            }
            if ($post_vars["vaccine"]) {
                $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
                foreach($post_vars["vaccine"] as $key=>$value) {
                    $sql = "insert into m_consult_ccdev_vaccine (ccdev_id, consult_id, user_id, patient_id, vaccine_timestamp, vaccine_id) ".
                           "values ('".$post_vars["ccdev_id"]."', '".$get_vars["consult_id"]."', '".$_SESSION["userid"]."', '$patient_id', sysdate(), '$value')";
                    $result = mysql_query($sql);
                }
            }
            break;
        case "Carry Out":
            break;
        }
    }

    function display_ccdev_firstvisit() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<b>FIRST VISIT DATA</b><br/><br/>";
        $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
        $sql = "select ccdev_id, patient_id, ".
               "mother_name, mother_educ_id, mother_occup_id, ".
               "father_name, father_educ_id, father_occup_id, ".
               "date_format(ccdev_timestamp, '%a %d %b %Y, %h:%i%p'), ccdev_dob, ".
               "birth_weight, delivery_location, civil_registry_date ".
               "from m_patient_ccdev where patient_id = '$patient_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($cid, $pid, $mname, $meduc, $moccup, $fname, $feduc, $foccup, $ts, $dob, $bw, $loc, $regdate) = mysql_fetch_array($result);
                print "<table cellpadding='3' style='border: 1px dashed black'><tr><td>";
                print "REGISTRY ID: <b>$cid</b><br/>";
                print "DATE: <b>$ts</b><br/><br/>";
                print "MOTHER: $mname<br/>";
                print "Education: ".education::get_education_name($meduc)."<br/>";
                print "Occupation: ".occupation::get_occupation_name($moccup)."<br/><br/>";
                print "FATHER: $fname<br/>";
                print "Education: ".education::get_education_name($feduc)."<br/>";
                print "Occupation: ".occupation::get_occupation_name($foccup)."<br/><br/>";
                print "DELIVERY LOCATION: $loc<br/>";
                print "BIRTH WEIGHT: $bw KG<br/>";
                print "CIVIL REGISTRY DATE: $regdate<br/>";
                print "</td></tr></table><br/>";
            }
        }
    }

    function form_ccdev_firstvisit() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<table width='300'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=VISIT1' name='form_consult_ccdev' method='post'>";
        print "<tr valign='top'><td>";
        print "<b>".FTITLE_CCDEV_DATA_FORM."</b><br/><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        // MATERNAL DATA
        print "<table bgcolor='#FFCCFF' width='300' cellpadding='3'><tr><td>";
        if (!isset($post_vars["mother_name"])) {
            $post_vars["mother_name"] = healthcenter::get_mothers_name($get_vars["consult_id"]);
        }
        print "<span class='boxtitle'>".LBL_MOTHERS_NAME."</span><br> ";
        print "<input type='text' size='30' class='textbox' ".($_SESSION["isadmin"]||!$get_vars["patient_id"]?"":"disabled")." name='mother_name' value='".($ccdev["mother_name"]?$ccdev["mother_name"]:$post_vars["mother_name"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_MOTHERS_OCCUPATION."</span><br> ";
        $sql_moccup = "select occup_id, occup_name from m_lib_occupation order by occup_name";
        if ($result = mysql_query($sql_moccup)) {
            if (mysql_num_rows($result)) {
                print  "<select size='5' name='mother_occup' class='textbox'>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<option value='$id' ".($ccdev["mother_occup_id"]==$id?"selected":"").">$name</option>";
                }
                print "</select>";
            }
        }
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_MOTHERS_EDUCATION."</span><br> ";
        $sql_meduc = "select educ_id, educ_name from m_lib_education order by educ_name";
        if ($result = mysql_query($sql_meduc)) {
            if (mysql_num_rows($result)) {
                print "<select size='5' name='mother_educ' class='textbox'>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<option value='$id' ".($ccdev["mother_educ_id"]==$id?"selected":"").">$name</option>";
                }
                print "</select>";
            }
        }
        print "</td></tr>";
        print "</table>";

        print "</td></tr>";
        print "<tr valign='top'><td>";
        // PATERNAL DATA
        print "<table bgcolor='#99CCFF' width='300' cellpadding='3'><tr><td>";
        print "<span class='boxtitle'>".LBL_FATHERS_NAME."</span><br> ";
        print "<input type='text' size='30' class='textbox' ".($_SESSION["isadmin"]||!$get_vars["patient_id"]?"":"disabled")." name='father_name' value='".($ccdev["father_name"]?$ccdev["father_name"]:$post_vars["father_name"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_FATHERS_OCCUPATION."</span><br> ";
        $sql_moccup = "select occup_id, occup_name from m_lib_occupation order by occup_name";
        if ($result = mysql_query($sql_moccup)) {
            if (mysql_num_rows($result)) {
                print  "<select size='5' name='father_occup' class='textbox'>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<option value='$id' ".($ccdev["father_occup_id"]==$id?"selected":"").">$name</option>";
                }
                print "</select>";
            }
        }
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_FATHERS_EDUCATION."</span><br> ";
        $sql_meduc = "select educ_id, educ_name from m_lib_education order by educ_name";
        if ($result = mysql_query($sql_meduc)) {
            if (mysql_num_rows($result)) {
                print "<select size='5' name='father_educ' class='textbox'>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<option value='$id' ".($ccdev["father_educ_id"]==$id?"selected":"").">$name</option>";
                }
                print "</select>";
            }
        }
        print "</td></tr>";
        print "</table>";

        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_BIRTH_WEIGHT."</span><br> ";
        print "<input type='text' class='textbox' size='10' maxlength='10' name='birth_weight' value='".($ccdev["birth_weight"]?$ccdev["birth_weight"]:$post_vars["birth_weight"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_CIVIL_REGISTRATION_DATE."</span><br> ";
        print "<input type='text' class='textbox' size='10' maxlength='10' name='civil_regdate' value='".($ccdev["civil_registry_date"]?$ccdev["civil_registry_date"]:$post_vars["civil_registry_date"])."' style='border: 1px solid #000000'><br>";
        print "<small>Use format MM/DD/YYYY.</small><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_DELIVERY_LOCATION."</span><br> ";
        print ccdev::show_delivery_location();
        print "</td></tr>";
        print "<tr><td>";
        if ($_SESSION["priv_add"]) {
            print "<br><input type='submit' value = 'Save Child Care Data' class='textbox' name='submitccdev' style='border: 1px solid #000000'><br>";
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";
    }

    function show_delivery_location() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $location_id = $arg_list[0];
        }
        $ret_val .= "<select name='delivery_location' class='textbox'>";
        $ret_val .= "<option value=''>Select Location</option>";
        $ret_val .= "<option value='HOME'>Home</option>";
        $ret_val .= "<option value='HOSP'>Hospital</option>";
        $ret_val .= "<option value='LYIN'>Lying-In Clinic</option>";
        $ret_val .= "</select>";
        return $ret_val;
    }

    function _details_ccdev() {
    //
    // main submodule for ccdev consults
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ccdev')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
        print "<b>PATIENT DATA</b><br/><br/>";
        print "AGE IN WEEKS: ".ccdev::get_age_weeks($patient_id)."<br/>";
        print "WEIGHT FOR AGE: <br/>";
        print "<br/>";
        print "<b>VACCINE RECORD</b><br/><br/>";
        $sql = "select ccdev_id, vaccine_id, vaccine_timestamp, date_format(vaccine_timestamp,'%a %d %b %Y') ".
               "from m_consult_ccdev_vaccine ".
               "where patient_id = '$patient_id' order by vaccine_id, vaccine_timestamp desc";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($cid, $vaccine, $vstamp, $vdate) = mysql_fetch_array($result)) {
                    print "<img src='../images/arrow_redwhite.gif' border='0'/> ";
                    print "<a href='".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ccdev=".$get_vars["ccdev"]."&vaccine=$vaccine&ts=$vstamp&ccdev_id=$cid#detail'>$vaccine</a> $vdate<br/>";
                    if ($get_vars["vaccine"]==$vaccine && $get_vars["ts"]==$vstamp && $get_vars["ccdev_id"]==$cid) {
                        ccdev::show_vaccine_details($menu_id, $post_vars, $get_vars);
                    }
                }
            } else {
                print "<font color='red'>No records</font><br/>";
            }
        }
        print "<br/>";
        print "<b>SERVICE RECORD</b><br/><br/>";
        $sql = "select service_id, date_format(ccdev_timestamp,'%a %d %b %Y') ".
               "from m_consult_ccdev_services ".
               "where patient_id = '$patient_id' order by service_id, ccdev_timestamp desc";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($service, $ts) = mysql_fetch_array($result)) {
                    print "<img src='../images/arrow_redwhite.gif' border='0'/> ".ccdev::get_service_name($service)." $ts<br/>";
                }
            } else {
                print "<font color='red'>No records</font><br/>";
            }
        }
    }

    function show_vaccine_details() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $sql = "select ccdev_id, consult_id, user_id, patient_id, vaccine_timestamp, date_format(vaccine_timestamp, '%a %d %b %Y, %h:%i%p'), vaccine_id ".
               "from m_consult_ccdev_vaccine where ".
               "ccdev_id = '".$get_vars["ccdev_id"]."' and vaccine_id = '".$get_vars["vaccine"]."' and ".
               "vaccine_timestamp = '".$get_vars["ts"]."'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($cdid, $cid, $uid, $pid, $vstamp, $vdate, $vid) = mysql_fetch_array($result);
                print "<a name='detail'>";
                print "<table width='250' cellpadding='3' style='border:1px dashed black'><tr><td>";
                print "<form>";
                print "<span class='tinylight'>";
                print "REGISTRY ID: $cdid<br/>";
                print "VACCINE: ".vaccine::get_vaccine_name($vid)."<br/>";
                print "VACCINE DATE: $vdate<br/>";
                print "AGE ON VACCINATION: ".ccdev::age_on_vaccination($pid, $vstamp)." weeks<br/>";
                print "GIVEN BY: ".user::get_username($uid)."<br/>";
                print "<input type='submit' name='submitdetail' value='Delete' class='tinylight' style='border: 1px solid black'/> ";
                print "<input type='submit' name='submitdetail' value='Update' class='tinylight' style='border: 1px solid black'/> ";
                print "</span>";
                print "</form>";
                print "</td></tr></table>";
            }
        }
    }

    function age_on_vaccination() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $patient_id = $arg_list[0];
            $vaccine_timestamp = $arg_list[1];
        }
        $sql = "select (to_days('$vaccine_timestamp') - to_days(patient_dob))/7 from m_patient where patient_id = '$patient_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($age) = mysql_fetch_array($result);
                return $age;
            }
        }

    }

    function _ccdev_services() {
    //
    // library submodule for ccdev services
    // calls form_service()
    //       display_service()
    //       process_service()
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ccdev')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $c = new ccdev;
        if ($post_vars["submitservice"]) {
            $c->process_service($menu_id, $post_vars, $get_vars);
        }
        $c->display_service($menu_id, $post_vars, $get_vars);
        $c->form_service($menu_id, $post_vars, $get_vars);
    }

    function show_services() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $service_id = $arg_list[0];
        }
        $sql = "select service_id, service_name from m_lib_ccdev_services order by service_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                $ret_val .= "<select name='service' class='textbox'>";
                $ret_val .= "<option value=''>Select Service</option>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    $ret_val .= "<option value='$id'>$name</option>";
                }
                $ret_val .= "</select>";
                return $ret_val;
            }
        }
    }

    function checkbox_services() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $service_id = $arg_list[0];
        }
        $sql = "select service_id, service_name from m_lib_ccdev_services order by service_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    $ret_val .= "<input type='checkbox' name='services[]' value='$id'> $name<br>";
                }
                return $ret_val;
            }
        }
    }

    function get_service_name() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $service_id = $arg_list[0];
        }
        $sql = "select service_name from m_lib_ccdev_services where service_id = '$service_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($name) = mysql_fetch_array($result);
                return $name;
            }
        }
    }

    function form_service() {
    //
    // called by _ccdev_services()
    //
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            if ($get_vars["service_id"]) {
                $sql = "select service_id, service_name ".
                       "from m_lib_ccdev_services where service_id = '".$get_vars["service_id"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $service = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id' name='form_service' method='post'>";
        print "<tr valign='top'><td>";
        print "<span class='library'>".FTITLE_CCDEV_SERVICE_FORM."</span><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_SERVICE_ID."</span><br> ";
        if ($get_vars["service_id"]) {
            $disable = "disabled";
            print "<input type='hidden' name='service_id' value='".$type["service_id"]."'>";
        } else {
            $disable = "";
        }
        print "<input type='text' class='textbox' size='10' $disable maxlength='10' name='service_id' value='".($service["service_id"]?$service["service_id"]:$post_vars["service_id"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_SERVICE_NAME."</span><br> ";
        print "<input type='text' class='textbox' size='15' maxlength='25' name='service_name' value='".($service["service_name"]?$service["service_name"]:$post_vars["service_name"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["service_id"]) {
            print "<input type='hidden' name='service_id' value='".$get_vars["service_id"]."'>";
            if ($_SESSION["priv_update"]) {
                print "<input type='submit' value = 'Update Service' class='textbox' name='submitservice' style='border: 1px solid #000000'> ";
            }
            if ($_SESSION["priv_delete"]) {
                print "<input type='submit' value = 'Delete Service' class='textbox' name='submitservice' style='border: 1px solid #000000'> ";
            }
        } else {
            if ($_SESSION["priv_add"]) {
                print "<input type='submit' value = 'Add Service' class='textbox' name='submitservice' style='border: 1px solid #000000'> ";
            }
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function process_service() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        if ($post_vars["submitservice"]) {
            if ($post_vars["service_id"] && $post_vars["service_name"]) {
                switch($post_vars["submitservice"]) {
                case "Add Service":
                    $sql = "insert into m_lib_ccdev_services (service_id, service_name) ".
                           "values ('".strtoupper($post_vars["service_id"])."', '".$post_vars["service_name"]."')";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Update Service":
                    $sql = "update m_lib_ccdev_services set ".
                           "service_name = '".$post_vars["service_name"]."' ".
                           "where service_id = '".$post_vars["service_id"]."'";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Delete Service":
                    if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                        $sql = "delete from m_lib_ccdev_services where service_id = '".$post_vars["service_id"]."'";
                        if ($result = mysql_query($sql)) {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                        }
                    } else {
                        if ($post_vars["confirm_delete"]=="No") {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]);
                        }
                    }
                    break;
                }
            }
        }
    }

    function display_service() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        print "<table width='400'>";
        print "<tr valign='top'><td colspan='3'>";
        print "<span class='library'>".FTITLE_CCDEV_SERVICES_LIST."</span><br>";
        print "</td></tr>";
        print "<tr valign='top'><td><b>".THEAD_ID."</b></td><td><b>".THEAD_NAME."</b></td></tr>";
        $sql = "select service_id, service_name from m_lib_ccdev_services order by service_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td>$id</td><td><a href='".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&service_id=$id'>$name</a></td></tr>";
                }
            }
        }
        print "</table><br>";
    }


// end of class
}
?>
